package com.lz.base.project.io;

import cn.hutool.core.date.StopWatch;
import com.lz.base.common.utils.common.SysUtils;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

public class Asyn {

    public static void main(String[] args) {
        String p =   "src/main/resources/templates/monitor.json";
        Path path = Paths.get(p);
        try (AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ)) {

            ByteBuffer buffer = ByteBuffer.allocate(1024);
            Future<Integer> operation = fileChannel.read(buffer, 0);
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            // 你可以继续做其他工作，当读操作完成后处理结果
            while (!operation.isDone()) {




                // 执行一些其他任务
            }
            stopWatch.stop();
            System.out.println("处理其他事情："+stopWatch.getTotalTimeNanos());
            // 读取完成后，你可以处理数据
            int bytesRead = operation.get();  // 阻塞直到读取完成
            System.out.println("Read " + bytesRead + " bytes");

            // 操作 buffer 中的数据...
            System.out.println(new String(buffer.array(), 0, buffer.limit()));

            System.out.println( buffer.toString());
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }
}
